﻿using System.Drawing;
namespace Library.Winform.Draw.Geometry {
  /// <summary>
  /// Line in form: Ax + By = C
  /// </summary>
  public struct Line {
    public int A { get; private set; }
    public int B { get; private set; }
    public int C { get; private set; }
    public Point From { get; private set; }
    public Point To { get; private set; }
    public void BuildLine(Point point) {
      BuildLine(Point.Empty, point);
    }
    public void BuildLine(Point from, Point to) {
      A = from.Y - to.Y;
      B = to.X - from.X;
      C = from.X * (from.Y - to.Y) - from.Y * (from.X - to.X);
      From = from;
      To = to;
    }
    public bool Intersect(Line line, out PointF intersect) {
      intersect = PointF.Empty;
      var det = A * line.B - line.A * B;
      if (det == 0) return false;
      intersect.X = (float)(line.B * C - B * line.C) / det;
      intersect.Y = (float)(A * line.C - line.A * C) / det;
      return true;
    }
  }
}
